<?php
/**
 * 后台管理 - 统计
 * 
 * @version $Id$
 */

class Module_statistics
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}
	
	/**
	 * 在线
	 */
	function online() {
		$day = isset($_GET['day']) ? trim($_GET['day']) : date('Y-m-d');
		$time_now = time();
		
		$smarty = loadSmarty();
		$Db = loadLib('Db');
		
		// 获取在线数据
		$start_ts = strtotime($day);
		$end_ts = $start_ts + 86400;
		$result = $Db->getAll("SELECT * FROM `statistics_online` WHERE `time` >= {$start_ts} AND `time` < {$end_ts} ORDER BY `time` ASC" , 'time');
		
		// 格式化成图标模式
		$onlines = array();
		for($time_ts = $start_ts ; $time_ts < $end_ts ; $time_ts = $time_ts + 300) {
			if(isset($result[$time_ts])) {
				$onlines[] = $result[$time_ts];
			} else {
				$onlines[] = array(
					'time' => $time_ts,
					'skeys' => 0,
					'unique_uins' => 0,
				);
			}
			
		}
		
		$smarty->assign('onlines' , $onlines);
		$smarty->assign('cur_day' , date('Y-m-d l' , strtotime($day) ) );
		
		return $smarty->fetch('statistics/online.tpl.php');
	}
	
	
	/**
	 * 登陆
	 */
	function in() {
		$mobile = isset($_POST['mobile']) ? trim($_POST['mobile']) : null;
		$password = isset($_POST['password']) ? intval($_POST['password']) : null;
		
		// 检查手机号码有效性
		if(empty($mobile) && preg_match('%^[0-9]{13}$%' , $mobile)) {
			return View::error('没有填写手机号码，或手机号码填写有误');
		}
		
		// 检查密码有效性
		if($password === null || strlen($password) == 0) {
			return View::error('没有填写密码');
		}
		
		loadLib('User');
		$result = User::checkPassword($mobile , $password);
		
		if(empty($result)) {
			return View::error('密码错误');
		}
		
		$uin = $result['uin'];
		
		// 白名单检查
		$white_list = array(
			1,
			3,
			9,
		);
		
		if(!in_array($uin , $white_list)) {
			return View::error('没有权限进入后台');
		}
		
		session_start();
		$_SESSION['adminid'] = $uin;
		
		header('Location: ' . ADMINURL);
	}
}

# end of script
